//
//  YBMineActivationCodeCell.swift
//  Moonlight
//
//  Created by Penlon Kim on 2023/7/2.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

import UIKit
import SnapKit

class YBMineActivationCodeCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .none
        self.backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        let line = UIView(color: kRGBColor(191, 190, 190))
        let productNameTitleLabel = self.bk_addLabel(text: "兑换套餐名称：", font: .systemFont(ofSize: 14), bgColor: .clear, textColor: .lightBlack51DarkLight230)
        let generateTimeTitleLabel = self.bk_addLabel(text: "激活码生成时间", font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray184)
        let activeTimeTitleLabel = self.bk_addLabel(text: "激活码激活时间", font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray184)
        let overdueTimeTitleLabel = self.bk_addLabel(text: "激活码到期时间", font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray184)
        
        
        contentView.addSubview(_contentView)
        _contentView.addSubviews([titleStatusLabel, validTimeLabel, line, productNameTitleLabel, productNameLabel, generateTimeTitleLabel, generateTimeLabel, activeTimeTitleLabel, activeTimeLabel, overdueTimeTitleLabel, overdueTimeLabel])
        
        _contentView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.right.equalToSuperview().inset(10)
        }
        
        titleStatusLabel.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(20)
            make.height.equalTo(18)
        }
        
        validTimeLabel.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.centerY.equalTo(titleStatusLabel)
            make.height.equalTo(18)
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(0.3)
            make.top.equalTo(titleStatusLabel.snp.bottom).offset(7)
        }
        
        productNameTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.height.equalTo(18)
            make.top.equalTo(line.snp.bottom).offset(7)
        }
        
        productNameLabel.snp.makeConstraints { make in
            make.centerY.equalTo(productNameTitleLabel)
            make.height.equalTo(18)
            make.right.equalTo(-20)
            make.left.equalTo(productNameTitleLabel.snp.right).offset(10)
        }
        
        generateTimeTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.height.equalTo(18)
            make.top.equalTo(productNameTitleLabel.snp.bottom).offset(7)
        }
        
        generateTimeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(generateTimeTitleLabel)
            make.right.equalTo(-20)
            make.left.equalTo(generateTimeTitleLabel.snp.right).offset(10)
        }
        
        activeTimeTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.height.equalTo(18)
            make.top.equalTo(generateTimeTitleLabel.snp.bottom).offset(7)
        }
        
        activeTimeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(activeTimeTitleLabel)
            make.right.equalTo(-20)
            make.left.equalTo(activeTimeTitleLabel.snp.right).offset(10)
        }
        
        overdueTimeTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.height.equalTo(18)
            make.top.equalTo(activeTimeTitleLabel.snp.bottom).offset(7)
            make.bottom.equalTo(-10)
        }
        
        overdueTimeLabel.snp.makeConstraints { make in
            make.centerY.equalTo(overdueTimeTitleLabel)
            make.right.equalTo(-20)
            make.left.equalTo(overdueTimeTitleLabel.snp.right).offset(10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    private lazy var _contentView: UIView = {
        let view = UIView(color: .lightWhiteDark33)
        view.bk_addStyleWith(cornerRadius: 4, corners: .allCorners)
        return view
    }()
    
    /// 激活码
    private lazy var titleStatusLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear)
    /// 有效期
    private lazy var validTimeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: .red)
    /// 兑换套餐名称
    private lazy var productNameLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: .lightBlack51DarkLight230, align: .right)
    /// 激活码生成时间
    private lazy var generateTimeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray184, align: .right)
    /// 激活码激活时间
    private lazy var activeTimeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray184, align: .right)
    /// 激活码到期时间
    private lazy var overdueTimeLabel = self.bk_addLabel(font: .systemFont(ofSize: 14), bgColor: .clear, textColor: ColorDef.gray184, align: .right)
    
}

// MARK: - Public
extension YBMineActivationCodeCell {
    
    func updateCell(by model: YBActivationFrontModel) {
        var status = "未使用"
        if model.status == 1 {
            status = "已使用"
        }else if model.status == 2 {
            status = "已过期"
        }
        titleStatusLabel.attributedText = NSMutableAttributedString("激活码 ") { $0
            .foregroundColor(.lightBlack51DarkLight230)
        }.addText(status) { $0
            .foregroundColor(.red)
        }
        
        validTimeLabel.text = "有效期：\(model.timeLimit)天"
        productNameLabel.text = model.goodsName
        generateTimeLabel.text = model.startTime
        activeTimeLabel.text = model.activateTime
        overdueTimeLabel.text = model.endTime
    }
    
}
